<template>
  <a-modal
    title="新建"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="modalOk"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="model && model.id > 0" label="主键ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="姓名">
          <a-input v-decorator="['name', {rules: [{required: true, message: '请输入姓名'}], initialValue: model.name}]" />
        </a-form-item>
        <a-form-item label="性别">
          <a-radio-group v-decorator="['gender', {rules: [{required: true, message: '请选择性别'}], initialValue: (model.gender === '男') ? 0 : 1}]">
            <a-radio :value="0">男</a-radio>
            <a-radio :value="1">女</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="父级" v-show="false">
          <a-input v-decorator="['parentId', {rules: [{required: false, message: '请输入至少五个字符的规则描述！'}], initialValue: model.parentId || 0}]" />
        </a-form-item>
        <a-form-item label="分销码" v-show="model && model.id">
          <a-input :disabled="true" v-decorator="['code', {rules: [{required: false, message: '请输入分销码'}], initialValue: model.code}]" />
        </a-form-item>
        <a-form-item label="手机号">
          <a-input v-decorator="['cellPhone', {rules: [{required: true, message: '请输入手机号'}], initialValue: model.cellPhone}]" />
        </a-form-item>
        <a-form-item label="是否管理员">
          <a-radio-group v-decorator="['isAdmin', {rules: [{required: true, message: '请选择是否管理员'}], initialValue: model.isAdmin}]">
            <a-radio :value="0">否</a-radio>
            <a-radio :value="1">是</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="部门">
          <a-select
            v-decorator="['departmentId', {rules: [{required: true, message: '请输入部门'}], initialValue: model.department && model.department.id}]"
          >
            <a-select-option
              v-for="(item, index) in departmentList"
              :key="index"
              :value="item.id"
            >
              {{ item.departName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="上级人员">
          <a-select
            show-search
            :value="parentMember.name"
            placeholder="input search text"
            :default-active-first-option="false"
            :show-arrow="false"
            :filter-option="false"
            :not-found-content="null"
            @search="memberHandleSearch"
            @change="memberHandleChange"
          >
            <a-select-option v-for="(d, index) in memberList" :key="index">
              {{ d.name }}
            </a-select-option>
          </a-select>
          <!-- <a-input :disabled="true" :value="parentMember.name" /> -->
          <a-input type="hidden" v-decorator="['parentId', {rules: [{required: true, message: '请输入token'}], initialValue: model.parentId}]" />
        </a-form-item>
        <a-form-item label="Token">
          <a-input v-decorator="['token', {rules: [{required: true, message: '请输入token'}], initialValue: model.token}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { getMember, getMemberLists, addMember, putMember, getDepartmentsLists } from '@/api/manage'
const fields = ['id']

export default {
  name: 'MemberForm',
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      selectMember: {},
      form: this.$form.createForm(this),
      parentMember: {},
      memberList: [],
      departmentList: []
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    const _self = this
    this.$watch('model', () => {
      this.form = this.$form.createForm(this)
      getDepartmentsLists().then(res => {
        this.departmentList = res.data
      })
      getMemberLists().then(res => {
        this.memberList = res.data
        this.memberList.unshift({ id: 0, name: '无上级' })
      })
      getMember(this.model.parentId).then(res => {
        this.parentMember = res.data
      })
      _self.$nextTick(() => {
        this.model && this.form.setFieldsValue(pick(this.model, fields))
      })
    })
  },
  methods: {
    memberHandleChange (index) {
      this.parentMember = this.memberList[index]
      this.form.setFieldsValue({ parentId: this.parentMember.id })
    },
    memberHandleSearch (value) {
      // console.log(value)
      getMemberLists({ search: value }).then(res => {
        this.memberList = res.data
      })
    },
    modalOk () {
      this.form.validateFieldsAndScroll((errors, values) => {
        if (!errors) {
          delete values.id
          if (this.model && this.model.id) {
            putMember(this.model.id, values).then(res => {
              this.$emit('ok')
            })
          } else {
            addMember(values).then(res => {
              this.$emit('ok')
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
